#include "../SocketCommon/common.h"
#pragma comment(lib,"../Debug/SocketCommon.lib")
#pragma comment(lib,"WS2_32.lib")

int main()
{
	SOCKET server;
	SOCKET incc;
	struct sockaddr_in sa;
	char buf[256];
	int len;
	int ir;

	init();
	
	server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(server==INVALID_SOCKET){
		ERROR_EXIT("socket");
	}

	bzero(&sa,sizeof(sa));
	sa.sin_addr.s_addr = htonl(INADDR_ANY);
	sa.sin_family = AF_INET;
	sa.sin_port = htons(1234);

	ir = bind(server,(const struct sockaddr*)&sa,sizeof(sa));
	if(ir<0){
		ERROR_EXIT("bind");
	}

	ir = listen(server,SOMAXCONN);
	if(ir<0){
		ERROR_EXIT("listen");
	}

	for(;;){
		incc = accept(server,NULL,NULL);
		if(incc==INVALID_SOCKET){
			ERROR_EXIT("accept");
		}

		printf("recieve incoming transmission...");

		bzero(buf,256);
		sprintf(buf,"hello client");

		len = send(incc,buf,256,0);
		if(len<0){
			ERROR_EXIT("send");
		}

		printf("done.\n");

		closesocket(incc);
	}

	fin();
	system("PAUSE");
	return 0;
}
